﻿// Decompiled with JetBrains decompiler
// Type: LayersParser.Objects.RRSConfiguration
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.OLPEditor.LayersParser.Resources;
using LayersParser.Extensions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using System.Xml.Linq;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;


namespace LayersParser.Objects;

public class RRSConfiguration : Common
{
  private XDocument _xDocument;
  private List<CustomizedPathConfiguration> customizedPaths = new List<CustomizedPathConfiguration>();

  public RRSConfiguration()
  {
    TxOLPUtilities txOlpUtilities = new TxOLPUtilities();
    TxTypeFilter txTypeFilter = new TxTypeFilter(typeof (ITxRobot));
    TxOlpControllerUtilities controllerUtilities = new TxOlpControllerUtilities();
    TxObjectList allDescendants = TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) txTypeFilter);
    TxDocument activeDocument = TxApplication.ActiveDocument;
    ArrayList arrayList = new ArrayList();
    foreach (TxRobot txRobot in (Collection<ITxObject>) allDescendants)
    {
      TxRobotRRSController controller = (TxRobotRRSController) txRobot.Controller;
      string name = txRobot.Controller.Name;
      if (!arrayList.Contains((object) name))
      {
        arrayList.Add((object) name);
        ArrayList installedVersions = txOlpUtilities.GetControllerInstalledVersions(name);
        if (installedVersions != null)
        {
          foreach (string version in installedVersions)
          {
            controller.Version = version;
            if (controllerUtilities.GetCustomizationPath((ITxRobotController) controller) != null)
              this.customizedPaths.Add(new CustomizedPathConfiguration(controllerUtilities.GetCustomizationPath((ITxRobotController) controller), version, name));
            else if (((TxRobotController) controller).Name == "default")
              this.customizedPaths.Add(new CustomizedPathConfiguration(txOlpUtilities.GetOLPInstallationDirectory() + "/Default/", "", name));
            else
              this.customizedPaths.Add(new CustomizedPathConfiguration($"{txOlpUtilities.GetOLPInstallationDirectory()}/{name}/", version, name));
          }
        }
        else
        {
          int num = (int) TxMessageBox.Show(name + LayersParserStringTable.NOT_DEFINED_IN_RRS_XML, LayersParserStringTable.PARAMETER_TYPE_LABEL, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
      }
    }
  }

  public List<CustomizedPathConfiguration> CustomizedPathConfigurations
  {
    get => this.customizedPaths;
    set
    {
      this.customizedPaths = value;
      this.SetXML();
    }
  }

  private void SetXML()
  {
    TxOLPUtilities txOlpUtilities = new TxOLPUtilities();
    foreach (CustomizedPathConfiguration customizedPath in this.customizedPaths)
    {
      if (!txOlpUtilities.GetControllerInstalledVersions(customizedPath.Controller).Contains((object) customizedPath.Name))
        this._xDocument.GoToControllerInstalledVersions(customizedPath.Controller).Add((object) customizedPath.Node);
    }
    this._xDocument.Save(txOlpUtilities.GetOLPInstallationDirectory() + "/rrs.xml");
  }
}
